// import notFound from '@/components/notFound.vue';
// // 全局导入组件
// const componentPath = [];
// const requireComponent = require.context(
//     // 其组件目录的相对路径
//     '@/mainModules/',
//     // 是否查询其子目录
//     true,
//     // 匹配基础组件文件名的正则表达式, 这里可以匹配的文件名为TdXxxx.vue格式
//     /.(vue)$/
// );
// requireComponent.keys().forEach(fileName => {
//     // 获取组件的路径
//     // 剥去文件名开头的 `'./` 和结尾的扩展名
//     componentPath.push(fileName.replace(/^\.\/(.*)\.\w+$/, '$1'));
// });
// //火狐下input readonly 光标显示问题
// window.$(document).on('focus', 'input[readonly]', function() {
//     this.blur();
// });
let routerFlag = false;
let topMenuId = '';
let toRouterFlag = false;
export default {
    /**
     * @func  getRequest
     * @desc  获取页面传参，返回参数对象
     * @return {object} theRequest 参数对象
     */
    getRequest: function() {
        let url = location.search; // 获取url中"?"符后的字串
        let theRequest = {};
        if (url.indexOf('?') !== -1) {
            let str = url.substr(1);
            let strs = decodeURIComponent(str).split('&');
            for (let i = 0; i < strs.length; i++) {
                theRequest[strs[i].split('=')[0]] = unescape(strs[i].split('=')[1]);
            }
        }
        return theRequest;
    },
    /**
     * @func  parseParam
     * @desc  将跳转路径和参数拼接为url返回
     * @param {object} url 需要转换的对象
     * @param {string} param   对象key值0
     * @return {string} url路径
     */
    getUrl: function(url, param) {
        return url + '?' + this.urlEncode(param);
    },
    /**
     * @func  parseParam
     * @desc  json转为url参数
     * @param {object} param 需要转换的对象
     * @param {string} key   对象key值
     * @param {string} encode   编码格式
     * @return {string} paramStr url参数
     */
    urlEncode: function(param, key, encode) {
        if (param == null) {
            return '';
        }
        let paramStr = '';
        let t = typeof param;
        if (t === 'string' || t === 'number' || t === 'boolean') {
            paramStr += '&' + encodeURIComponent(key) + '=' + (encode == null || encode ? encodeURIComponent(param) : param);
        } else {
            for (let i in param) {
                let k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i);
                paramStr += this.urlEncode(param[i], k, encode);
            }
        }
        return paramStr;
    },
    /**
     * @func  isEmpty
     * @desc  判断是否为空
     * @param {object} obj 需要判断的对象
     * @return {boolean} 是否为空
     */
    isEmpty: function(obj) {
        if (typeof obj === 'undefined' || obj === undefined || obj == null || obj === '') {
            return true;
        }
        return false;
    },
    /**
     * @func  isFunction
     * @desc  判断是否为方法
     * @param {object} it 判断的对象
     * @return {boolean} 是否
     */
    isFunction: function(it) {
        return Object.prototype.toString.call(it) === '[object Function]';
    },
    /**
     * @func  isArray
     * @desc  判断是否为数组
     * @param {object} it 判断的对象
     * @return {boolean} 是否
     */
    isArray: function(/* anything */ it) {
        return it && (it instanceof Array || typeof it === 'array'); // Boolean
    },
    /**
     * @func  clone
     * @desc  对象复制
     * @param {object} o 复制的对象
     * @return {object} 复制的对象
     */
    clone: function(/* anything */ o) {
        if (!o || typeof o !== 'object' || this.isFunction(o)) {
            return o; // anything
        }
        if (o.nodeType && 'cloneNode' in o) {
            return o.cloneNode(true);
        }
        if (o instanceof Date) {
            // Date
            return new Date(o.getTime());
        }
        let r, i, l, s, name;
        if (this.isArray(o)) {
            // array
            r = [];
            for (i = 0, l = o.length; i < l; ++i) {
                if (i in o) {
                    r.push(this.clone(o[i]));
                }
            }
        } else {
            r = o.constructor ? new o.constructor() : {};
        }
        let empty = {};
        for (name in o) {
            s = o[name];
            if (!(name in r) || (r[name] !== s && (!(name in empty) || empty[name] !== s))) {
                r[name] = this.clone(s);
            }
        }

        return r; // Object
    },
    /**
     * @func  mergeRoutes
     * @desc  拼接路由信息
     * @param {arrey} menus 菜单数组
     * @return {arrey} 路由数组
     */
    // mergeRoutes(menus, routes, indexId) {
    //     if (menus && menus.length > 0) {
    //         for (let i = 0; i < menus.length; i++) {
    //             if (menus[i].children.length > 0) {
    //                 this.mergeRoutes(menus[i].children, routes, indexId);
    //             } else {
    //                 let index = componentPath.indexOf(menus[i].menuUrl);
    //                 if (index > -1) {
    //                     routes.push({
    //                         path: '/' + menus[i].menuId,
    //                         name: menus[i].menuId,
    //                         component: resolve => require([`@/mainModules/${menus[i].menuUrl}.vue`], resolve),
    //                         meta: {
    //                             currentMenu: menus[i]
    //                         }
    //                     });
    //                     if (indexId && indexId == menus[i].menuId) {
    //                         routerFlag = true;
    //                     }
    //                 }
    //             }
    //         }
    //     }
    //     return routes;
    // },
    /**
     * @func  addRouter
     * @desc  添加路由
     * @param {arrey} menus 菜单数组
     * @param {arrey} router 路由对象
     * @param {arrey} indexId 所选菜单
     * @return {arrey} 路由数组
     */
    addRouter(menus, router, indexId) {
        router.push({
            path: '/notFound',
            meta: {
                requireAuth: true
            },
            component: notFound
        });
        const retInfo = [];
        routerFlag = false;
        let routes = this.mergeRoutes(menus, router, indexId);
        retInfo.push(routes);
        retInfo.push(routerFlag);
        return retInfo;
    },
    /**
     * @func  toRouter
     * @desc  跳转菜单
     * @param {arrey} routerId 路由id
     * @return {}
     */
    toRouter(routerId, param) {
        toRouterFlag = false;
        this.saveMenuIndexId(window.vue.$common.menuData, routerId, param);
    },
    saveMenuIndexId(menuData, routerId, param) {
        for (let i = 0; i < menuData.length; i++) {
            if (toRouterFlag) {
                return false;
            }
            if (menuData[i].parentMenuId === '3000') {
                topMenuId = menuData[i].menuId;
                window.vue.$common.topMenuObj = menuData[i];
            }
            if (menuData[i].children.length > 0) {
                this.saveMenuIndexId(menuData[i].children, routerId, param);
            } else {
                if (menuData[i].menuId === routerId) {
                    window.vue.$router.push({ name: routerId, params: { param } });
                    if (window.vue.$store.state.topMenuIndex === topMenuId) {
                        window.vue.$store.state.leftMenuIndex = routerId;
                    } else {
                        window.vue.$common.topMenuIdMap.set(topMenuId, menuData[i]);
                        window.vue.$store.state.topMenuIndex = topMenuId;
                    }
                    toRouterFlag = true;
                }
            }
        }
    },
    // 延时执行器
    delayExcute(func, delay) {
        let timer;
        return function(...args) {
            if (timer) {
                clearTimeout(timer);
            }
            timer = setTimeout(() => {
                func.apply(this, args);
            }, delay);
        };
    },
    //获取url参数
    getUrlKey(name) {
        return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20')) || null;
    },
    //生成uuid
    getUUID() {
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
            var r = (Math.random() * 16) | 0,
                v = c == 'x' ? r : (r & 0x3) | 0x8;
            return v.toString(16);
        });
    }
};
